import { Connection } from "../function/db/connection";
import { connectionManger } from "../function/db/connectionmanager";
import { mysqlTransaction } from "../function/db/mysqltransaction";
import { config } from "../function/dbconfig";
import { beginTransction, closeConnection, commitTransaciton, getConnection, rollbackTransaction } from "../function/db/transactionSavepoint";
import { Transaction } from "../function/db/transaction";
//初始化数据库
connectionManger.init(config)

async function tran1() {

    const conn = await getConnection()
    let tx : mysqlTransaction = connectionManger.getTransaction(conn)
    // tx.begin()
    await beginTransction()
    console.log('begin1');
    
    await tran2()
    // await tran3()
    let name = 'tran1'
    const query = 'INSERT INTO users (name) VALUES (?)';
    await new Promise((resolve, reject) => {
        conn.conn.query(query, [name], (error, results) => {
            if (error) {
                reject(error);
            } else {
                resolve(results);
            }
        });
    });
    // tx.commit()
    // tx.rollback()
    
    console.log('roll1');
    console.log(Transaction.getSavePointName());
    console.log(Transaction.nestingLevel);

    await commitTransaciton();
    // await rollbackTransaction()

    await closeConnection()
}

async function tran2() {

    const conn = await getConnection()
    // await f1()
    await beginTransction()


    await tran3()
    console.log('begin2');

    let name = 'tran2'
    const query = 'INSERT INTO users (name) VALUES (?)';

    await new Promise((resolve, reject) => {
        conn.conn.query(query, [name], (error, results) => {
            if (error) {
                reject(error);
            } else {
                resolve(results);
            }
        });
    });

    console.log('commit2');

    console.log(Transaction.getSavePointName());
    console.log(Transaction.nestingLevel);
    // await commitTransaciton();

    await rollbackTransaction()
    await closeConnection()
}

async function tran3() {

    const conn = await getConnection()
    // await f1()
    await beginTransction()

    console.log('begin3');

    let name = 'tran3'
    const query = 'INSERT INTO users (name) VALUES (?)';

    await new Promise((resolve, reject) => {
        conn.conn.query(query, [name], (error, results) => {
            if (error) {
                reject(error);
            } else {
                resolve(results);
            }
        });
    });

    console.log('commit3');

    console.log(Transaction.getSavePointName());
    console.log(Transaction.nestingLevel);
    
    
    await commitTransaciton();

    // await rollbackTransaction()
    await closeConnection()
}

tran1();




// async function f1() {
//     console.log('f1');
//     await f2();
// }

// async function f2() {
//     console.log('f2');
//     await f3();
// }

// async function f3() {
//     console.log('f3');
//     await getConnection();
// }
































// async function tran2(){
//     let connection: Connection = await connectionManger.createConnection()
//     //2
//     let tx: mysqlTransaction = connectionManger.getTransaction(connection)
//     // console.log(conn);

//     // console.log(tx);

//     await tx.begin();

//     let name = 'tran2'
//     const query = 'INSERT INTO users (name) VALUES (?)';
//     // 使用数据库连接池执行数据库插入操作
//     await new Promise((resolve, reject) => {
//         connection.conn.query(query, [name], (error, results) => {
//             if (error) {
//                 reject(error);
//             } else {
//                 resolve(results);
//             }
//         });
//     });

//     //事务提交
//     // await tx.commit();
//     //事务回滚
//     await tx.rollback();
//     await connectionManger.close(connection);
// }
// tran2()

